package cn.com.zpark.session;

import cn.com.zpark.session.bean.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @PackageName: cn.com.zpark.session
 * @ClassName: SessionDemo04
 * @Description:
 * @author: RZS
 * @date: 2021/11/16  18:08
 */
@WebServlet("/sd4.do")
public class SessionDemo04 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /**
         * 绑定数据到session中
         */
        // 创建user对象
        User user = new User();
        user.setName("张三");
        user.setPwd("root");

        // 获取session对象
        HttpSession session = request.getSession();
        // 绑定数据
        session.setAttribute("user", user);

        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("<h1>绑定数据成功</h1>");
    }
}
